package com.byq.mm.abstractx.cluster.client;

import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


import com.byq.mm.abstractx.cluster.provider.ClusterProviderManager;
import com.byq.mm.abstractx.device.Cluster;
import com.byq.mm.abstractx.device.Device;
import com.byq.mm.abstractx.engine.CoreEnvironment;


public class DeviceClient {

	Device device;
	Map<String, ClusterClient> clients = new HashMap<String, ClusterClient>();
	CoreEnvironment env;

	public Device getDevice() {
		return device;
	}

	public DeviceClient(Device device, CoreEnvironment e) {
		super();
		this.device = device;
		this.env = e;
		for (Cluster c : device.getSupportedClusters()) {
			Class<? extends ClusterClient> type = ClusterProviderManager
					.getClientType(c.getId());

			Constructor<? extends ClusterClient> con;
			try {
				con = type.getConstructor(String.class, Device.class, CoreEnvironment.class);
				ClusterClient cc = con.newInstance(c.getId(), device, env);
				clients.put(c.getId(), cc);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	}
//	public void interact(Interaction i){
//		
//	}

	public ClusterClient getClusterClient(String clusterId) {
		return clients.get(clusterId);
	}
	public Collection<ClusterClient> getClusterClients() {
		return clients.values();
	}

}
